import Pages from '../../pages/pages.vue'
import { showMsg, getInviteUserList, importInviteUserList, setMenuType, confirm } from '../../../vuex/action'
import importData from './import-data/import-data.vue'

export default {
  name: 'invite-list',
  data() {
    return {
      checkedPhones: [],
      checkAll: false,
      noSelectUser: true,
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      userList: [],
      phone: '',
      status: '',
      showImport: false
    }
  },
  components: {
    'vue-pages': Pages,
    'vue-import-data': importData
  },
  vuex: {
    getters: {
      fresh: state => state.inviteListFresh,
    },
    actions: {
      xShowMsg: showMsg,
      xSetMenuType: setMenuType,
      xGetInviteUserList: getInviteUserList,
      xImportInviteUserList: importInviteUserList,
      xConfirm: confirm
    }
  },
  ready() {
    this.xSetMenuType('activity')
    this.getList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit}) {
      this.xGetInviteUserList({
        start: start,
        limit: limit,
        targetPhone: this.phone,
        status: this.status
      })
        .then((data) => {
          this.userList = data.list
          this.pages.totalPages = Math.ceil(data.totalCount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    importData() {
      var file = this.$els.settlefile.files[0]
      if (file) {
        var formData = new FormData()
        formData.append('excelFile', file)
        this.xImportInviteUserList(formData)
          .catch((res) => {
            this.xShowMsg({
              type: 'danger',
              content: res.error_msg
            })
          })
      }
    },
    searchList() {
      this.getList({
        start: 0
      })
    },
    btnConfirm(phone) {
      var params = {
        phones: phone||this.checkedPhones.join()
      }
      this.xConfirm(params)
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    },
    checkAll() {
      this.checkedPhones = []
      for (var item of this.userList) {
        if (this.checkAll) {
          this.checkedPhones.push(item.targetPhone + '')
        }
      }
    },
    checkedPhones() {
      if (this.checkedPhones.length === 0) {
        this.noSelectUser = true
      } else {
        this.noSelectUser = false
      }
    }
  }
}
